import UIKit

public class HTMLTagParser {
    // HTML 标签类型
    public enum TagType:Equatable {
        case span
        case div
        case a
        case mark
        case ruby
        case rt
        case custom(String)
        
        var description: String {
            switch self {
            case .span: return "span"
            case .div: return "div"
            case .a: return "a"
            case .mark: return "mark"
            case .ruby: return "ruby"
            case .rt: return "rt"
            case .custom(let name): return name
            }
        }
        
        init(tagName: String) {
            switch tagName.lowercased() {
            case "span": self = .span
            case "div": self = .div
            case "a": self = .a
            case "mark": self = .mark
            case "ruby": self = .ruby
            case "rt": self = .rt
            default: self = .custom(tagName)
            }
        }
        
    }
    
    // HTML 标签信息
    public struct TagInfo {
        let type: TagType
        let attributes: [String: String]
        let content: String
        let children: [TagInfo]
        
        public init(type: TagType, attributes: [String : String], content: String, children: [TagInfo]) {
            self.type = type
            self.attributes = attributes
            self.content = content
            self.children = children
        }
    }
    
    // 解析 HTML
    public func parse(_ html: String) -> TagInfo? {
        // 匹配完整的 HTML 标签，包括嵌套内容
        let pattern = "<(\\w+)([^>]*)>([^<]*(?:(?!</\\1>)<[^<]*)*)</\\1>"
        
        guard let regex = try? NSRegularExpression(pattern: pattern),
              let match = regex.firstMatch(in: html, range: NSRange(html.startIndex..., in: html)) else {
            return nil
        }
        
        let tagName = (html as NSString).substring(with: match.range(at: 1))
        let attributesString = (html as NSString).substring(with: match.range(at: 2))
        let content = (html as NSString).substring(with: match.range(at: 3))
        
        return TagInfo(
            type: TagType(tagName: tagName),
            attributes: parseAttributes(attributesString),
            content: content,
            children: parseChildren(content)
        )
    }
    
    // 解析属性
    private func parseAttributes(_ attributesString: String) -> [String: String] {
        var attributes: [String: String] = [:]
        
        // 匹配属性，支持单引号和双引号
        let pattern = "(\\w+)=(['\"])(.*?)\\2"
        
        guard let regex = try? NSRegularExpression(pattern: pattern) else {
            return attributes
        }
        
        let matches = regex.matches(
            in: attributesString,
            range: NSRange(attributesString.startIndex..., in: attributesString)
        )
        
        for match in matches {
            let key = (attributesString as NSString).substring(with: match.range(at: 1))
            let value = (attributesString as NSString).substring(with: match.range(at: 3))
            attributes[key] = value
        }
        
        return attributes
    }
    
    // 解析子标签
    private func parseChildren(_ content: String) -> [TagInfo] {
        var children: [TagInfo] = []
        
        // 递归解析子标签
        if let childTag = parse(content) {
            children.append(childTag)
        }
        
        return children
    }
} 
